package com.example.ioc.context;

import com.example.common.Statistical;
import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;

@Slf4j
//@Component
public class LDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor {

    private static volatile LDestructionAwareBeanPostProcessor instance;

    private LDestructionAwareBeanPostProcessor(){
        Statistics.logInfo(log);
    }

    public static LDestructionAwareBeanPostProcessor getInstance(){
        if(instance == null){
            synchronized (LDestructionAwareBeanPostProcessor.class){
                if(instance == null){
                    instance = new LDestructionAwareBeanPostProcessor();
                }
            }
        }
        return instance;
    }

    @Override
    public void postProcessBeforeDestruction(@NonNull Object bean, @NonNull String beanName) throws BeansException {
        if (bean instanceof Statistical statistical) {
            Statistics.logInfo(log, statistical, "postProcessBeforeDestruction", bean);
        }
    }
}
